package com.doyutu.seed.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doyutu.seed.common.exception.BasicException;
import com.doyutu.seed.entity.SeedUser;
import com.doyutu.seed.service.SeedUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author DoyuTu
 * @version 0.0.1
 * spring-boot-project-seed
 */
@Api(tags = "SeedUser接口模块")
@RestController
@Slf4j
public class SeedUserController {

    @Autowired
    private SeedUserService seedUserService;

    @ApiOperation("获取所有Seeduser")
    @GetMapping(value = "getAllSeedUser", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object getAllSeedUser() {
        Wrapper<SeedUser> seedUserWrapper = new QueryWrapper<>();
        return seedUserService.getObj(seedUserWrapper);
    }

    @ApiOperation("插入Seeduser")
    @GetMapping(value = "insertSeedUser/{userName}/{type}/{state}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object insertSeedUser(@ApiParam("用户名") @PathVariable("userName") String userName,
                                 @ApiParam("类型") @PathVariable("type") String type,
                                 @ApiParam("状态") @PathVariable("state") String state) {
        SeedUser seedUser = new SeedUser()
                .setUserName(userName)
                .setState(state)
                .setType(type);
        return seedUserService.save(seedUser);
    }

    @GetMapping(value = "updateSeedUserById/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object updateSeedUserById(@PathVariable Integer id) {
        SeedUser seedUser = new SeedUser()
                .setId(id)
                .setType("");
        return seedUserService.updateById(seedUser);
    }

    @GetMapping("throwException")
    public Object throwException() {
        throw new BasicException("404", "出现异常");
    }

}
